package com.dingpeikeji.quickdeer.modules.rentprod.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.User;
import com.dingpeikeji.quickdeer.core.modules.sys.utils.UserUtils;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.modules.rentprod.entity.TRentprod;
import com.dingpeikeji.quickdeer.modules.rentprod.service.TRentprodService;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TCargotype;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TCargotypeForsale;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TCargotypeForsaleService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TCargotypeService;

/**
 * 整车调价Controller
 * @author mash
 * @version 2017-03-16
 */
@Controller
@RequestMapping(value = "${adminPath}/rentprod/tRentprod")
public class TRentprodController extends BaseController {

	@Autowired
	private TCargotypeForsaleService tCargotypeForsaleService;
	
	@Autowired
	private TCargotypeService tCargotypeService;
	
	@ModelAttribute
	public TCargotypeForsale get(@RequestParam(required=false) String id) {
		TCargotypeForsale entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tCargotypeForsaleService.get(id);
		}
		if (entity == null){
			entity = new TCargotypeForsale();
		}
		return entity;
	}
	
	@RequiresPermissions("rentprod:tRentprod:allview")
	@RequestMapping(value = {"alllist"})
	public String alllist(TCargotypeForsale tCargotypeForsale, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TCargotypeForsale> page = tCargotypeForsaleService.findPage(new Page<TCargotypeForsale>(request, response), tCargotypeForsale); 
		
		//获取车型数据
		List<TCargotype> cargotypelist = tCargotypeService.findList(new TCargotype());
		
		model.addAttribute("cargotypelist", cargotypelist);
		model.addAttribute("tRentprod", tCargotypeForsale);
		model.addAttribute("page", page);
		return "modules/rentprod/tRentprodAllList";
	}
	
	@RequiresPermissions("rentprod:tRentprod:view")
	@RequestMapping(value = {"list", ""})
	public String list(TCargotypeForsale tCargotypeForsale, HttpServletRequest request, HttpServletResponse response, Model model) {
		//查本公司的调价
		tCargotypeForsale.setOffice(UserUtils.getUser().getOffice());
		Page<TCargotypeForsale> page = tCargotypeForsaleService.findPage(new Page<TCargotypeForsale>(request, response), tCargotypeForsale); 
		
		//获取车型数据
		List<TCargotype> cargotypelist = tCargotypeService.findalltypes();
		
		model.addAttribute("cargotypelist", cargotypelist);
		model.addAttribute("tRentprod", tCargotypeForsale);
		model.addAttribute("page", page);
		return "modules/rentprod/tRentprodList";
	}

	@RequiresPermissions("rentprod:tRentprod:view")
	@RequestMapping(value = "form")
	public String form(TRentprod tRentprod, Model model) {
		//获取车型数据
		List<TCargotype> cargotypelist = tCargotypeService.findalltypes();
		
		model.addAttribute("cargotypelist", cargotypelist);
		model.addAttribute("tRentprod", tRentprod);
		return "modules/rentprod/tRentprodForm";
	}

}